#!/bin/bash
# This script does work currently needed to regenerate the sources found
# in Sources/GeneratedServices.

set -eu

# Where does this script live so we can hit things in the tree.
readonly ToolsDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")")
readonly ScriptName=$(basename "$0")

readonly GeneratedServicesDir="${ToolsDir}/../Sources/GeneratedServices"
readonly Configuration=debug
readonly ServiceGeneratorBinary="${ToolsDir}/.build/${Configuration}/ServiceGenerator"

BRIEF_MODE=--brief
if [[ $# -ge 1 && ( "$1" == "--no-brief" ) ]] ; then
  BRIEF_MODE=
  shift
fi
GENERATE_PREFERRED="yes"
if [[ $# -ge 1 && ( "$1" == "--no-preferred" ) ]] ; then
  GENERATE_PREFERRED="no"
  shift
fi
readonly EXTRA_OPTS=$*

# -----------------------------------------------------------------------------

# Helper to print a message and hopefully have it seen.
Banner() {
  echo ""
  echo "==============================================================================="
  echo "    $*"
  echo "==============================================================================="
  echo ""
}

# -----------------------------------------------------------------------------

Banner "Ensuring ServiceGenerator is up to date..."
( cd "${ToolsDir}" && swift build --configuration "${Configuration}" )

# -----------------------------------------------------------------------------

SERVICE_GENERATOR_COMMAND=(
  "${ServiceGeneratorBinary}"
    --outputDir "${GeneratedServicesDir}"
    --addServiceNameDir yes
    --removeUnknownFiles yes
    --gtlrFrameworkName GoogleAPIClientForREST
    --publicHeadersSubDir GoogleAPIClientForREST
    --auditJSON
    --guessFormattedNames
    --httpHeader X-User-IP:0.0.0.0
    ${BRIEF_MODE}
    # Cloud has a service named "Batch", which means GTLRBatchQuery would be the
    # base of the Query classes and thus conflict with the batching support, so
    # override the name to avoid the issues.
    --formattedName batch=CloudBatch

    # Include a filter so there are no warnings/infos during normal generation.
    --messageFilter "${ToolsDir}/${ScriptName}-message_filter.json"
)

if [[ "${GENERATE_PREFERRED}" == "yes" ]] ; then
  SERVICE_GENERATOR_COMMAND+=(
    # Generate everything flagged as preferred.
    --generatePreferred

    # At least one server from preferred is returning content-type: text/html.
    --parseTextHTMLReplies

    # The Admin SDK is odd, it is a bunch of 'admin' apis with different versions
    # to "group" them (https://www.googleapis.com/discovery/v1/apis?name=admin);
    # so only one gets marked as preferred. To generate all of it we have to
    # manually fetch anything not preferred which means updating this if there are
    # changes. admin:reports_v1 is currently the preferred one in discovery.
    'https://admin.googleapis.com/$discovery/rest?version=datatransfer_v1'
    'https://admin.googleapis.com/$discovery/rest?version=directory_v1'

    # Not sure why this is in discovery, turn it off.
    --skip prod_tt_sasportal
  )
fi

# Away it goes...
Banner "Running the ServiceGenerator..."
"${SERVICE_GENERATOR_COMMAND[@]}" ${EXTRA_OPTS}

# -----------------------------------------------------------------------------

Banner "Ensure the podspec is up to date..."
"${ToolsDir}/update_podspec_services.py"

# -----------------------------------------------------------------------------

Banner "Ensure Package.swift is up to date..."
"${ToolsDir}/update_swiftpm_services.py"
